//
//  SettlementController.m
//  ScalperMerchant
//
//  Created by 王云 on 2017/12/2.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "SettlementController.h"
#import "SettlementCell.h"
#import "HistorySettleController.h"

@interface SettlementController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak)UILabel *SingularNum;
@property (nonatomic,weak)UILabel *SettlPriceNum;
@property (nonatomic,weak)UIButton *ApplyBtn;

@property (nonatomic,weak)UITableView *tableV;

@property (nonatomic, assign) NSInteger PageNumber;
@property (nonatomic,strong)NSNumber *totalCountKey;
@property (nonatomic,strong)NSMutableArray *billData;
@end

@implementation SettlementController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"可结算明细";
     _billData  = [NSMutableArray array];
    
    [self setUI];
    [self loadData];
    [self loadReferView];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"历史结算单" style:UIBarButtonItemStylePlain target:self action:@selector(Historical)];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    [rightItem setTitleTextAttributes:dic forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = rightItem;
}
- (void)loadData{
    
    _PageNumber = 1;
    __weak __typeof(&*self)weakSelf = self;
    [SCApiTool ScEnableCurPage:_PageNumber pageSize:12 success:^(NSDictionary *dic) {
        weakSelf.totalCountKey = [dic hw_safeObjectForKey:@"count_page"];
        [weakSelf.SingularNum hw_setAttributedText:[NSString stringWithFormat:@"核销单数: <style font=17 color=#5aa54d>%@</style>",dic[@"count"]]];
        [weakSelf.SettlPriceNum hw_setAttributedText:[NSString stringWithFormat:@"可结算额:<style font=17 color=#fd6c40>%@</style>",dic[@"total_price"]]];
        
        NSArray *bill = [dic hw_safeObjectForKey:@"data_list"];
        if (bill.count == 0) {
            [SCTool showMessage:@"暂无数据"];
        }
        weakSelf.billData = bill.mutableCopy;
        [weakSelf performSelector:@selector(setBillData:) withObject:_billData];
        [weakSelf.tableV reloadData];
        
    } failure:^(NSString *errormsg) {
         [SCTool showError:errormsg];
    }];
}
- (void)loadReferView
{
    self.tableV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
- (void)setBillData:(NSMutableArray *)billData{
    _billData = billData;
    if ([_billData count] < 12) {
        self.tableV.mj_footer.hidden = YES;
    }else{
        [self.tableV.mj_footer setHidden:NO];
    }
}
- (void)loadMoreData{
    _PageNumber++;
    if ([_totalCountKey integerValue]< _PageNumber) {
        [SCTool showMessage:@"没有更多数据"];
        [_tableV.mj_footer endRefreshing];
        _PageNumber--;
        return ;
    }
    __weak __typeof(&*self)weakSelf = self;
    [SCApiTool ScEnableCurPage:_PageNumber pageSize:12 success:^(NSDictionary *dic) {
        
        [weakSelf.billData addObjectsFromArray:[dic hw_safeObjectForKey:@"data_list"]];
        [weakSelf.tableV reloadData];
        [weakSelf.tableV.mj_footer endRefreshing];
        
    } failure:^(NSString *errormsg) {
        [SCTool showMessage:errormsg];
        [weakSelf.tableV.mj_footer endRefreshing];
        
    }];
    
    
}


- (void)Historical{
    HistorySettleController *hisVC = [[HistorySettleController alloc] init];
    [self.navigationController pushViewController:hisVC animated:YES];
}
- (void)applyClick{
    if (_billData.count == 0 ) {
        [SCTool showMessage:@"无可结算单明细"];
        return;
    }
    
    __weak __typeof(&*self)weakSelf = self;
    [SCTool showAlerts:@"确定申请结算吗" Ok:^(id alerts) {
        
        [SCApiTool ScApplySuccess:^(NSString *msg) {
            [SCTool showSuccess:msg];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                HistorySettleController *hisVC = [[HistorySettleController alloc] init];
                [weakSelf.navigationController pushViewController:hisVC animated:YES];
            });
        } failure:^(NSString *errormsg) {
            [SCTool showError:errormsg];
        }];

        
    } Cancel:^(id alerts) {
        
    }];
    
}
- (void)setUI{
    
    UIView *BgView = [[UIView alloc] init];
    BgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:BgView];
    
    UILabel *SingularNum = [[UILabel alloc] init];
    [SingularNum setTextAlignment:NSTextAlignmentCenter];
    SingularNum.font = [UIFont systemFontOfSize:14];
    SingularNum.attributedText = [WYNSString Att:@"核销单数: 5" rang:@"5" font:[UIFont systemFontOfSize:17] color:WYColor(84, 164, 89) ParagraphSpacing:0];
    [BgView addSubview:SingularNum];
    _SingularNum = SingularNum;
    
    UIImageView *imgVL = [[UIImageView alloc] init];
    imgVL.backgroundColor = WYColor(84, 164, 89);
    [BgView addSubview:imgVL];
    
    UILabel *SettlPriceNum = [[UILabel alloc] init];
    [SettlPriceNum setTextAlignment:NSTextAlignmentCenter];
    SettlPriceNum.font = [UIFont systemFontOfSize:14];
    SettlPriceNum.attributedText = [WYNSString Att:@"可结算额: 120" rang:@"120" font:[UIFont systemFontOfSize:17] color:KOrange ParagraphSpacing:0];
    [BgView addSubview:SettlPriceNum];
    _SettlPriceNum = SettlPriceNum;
    
    UIImageView *imgVR = [[UIImageView alloc] init];
    imgVR.backgroundColor = WYColor(84, 164, 89);
    [BgView addSubview:imgVR];
    
    UIButton *ApplyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [ApplyBtn setTitle:@"申请结算" forState:UIControlStateNormal];
    [ApplyBtn addTarget:self action:@selector(applyClick) forControlEvents:UIControlEventTouchUpInside];
    ApplyBtn.layer.cornerRadius = 3;
    ApplyBtn.layer.borderWidth = 1;
    ApplyBtn.layer.borderColor = KOrange.CGColor;
    [ApplyBtn setTitleColor:KOrange forState:UIControlStateNormal];
    ApplyBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [BgView addSubview:ApplyBtn];
    
    [self.view addSubview:self.tableV];
    
    __weak __typeof(&*self)weakSelf = self;
    [BgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view.mas_top).offset(4);
        make.left.right.equalTo(weakSelf.view);
        make.height.equalTo(@60);
    }];
    [SingularNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(BgView);
        make.left.equalTo(BgView.mas_left);
        make.width.equalTo(BgView.mas_width).multipliedBy(0.333);
    }];
    [imgVL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(BgView);
        make.left.equalTo(SingularNum.mas_right);
        make.size.mas_equalTo(CGSizeMake(1, 20));
    }];
    [imgVL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(BgView);
        make.left.equalTo(SingularNum.mas_right);
        make.size.mas_equalTo(CGSizeMake(1, 20));
    }];
    [SettlPriceNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(BgView);
        make.left.equalTo(imgVL.mas_right);
        make.width.equalTo(BgView.mas_width).multipliedBy(0.333);
    }];
    [imgVR mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(BgView);
        make.left.equalTo(SettlPriceNum.mas_right);
        make.size.mas_equalTo(CGSizeMake(1, 20));
    }];
    [ApplyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(BgView);
        make.size.mas_equalTo(CGSizeMake(70, 30));
        make.right.equalTo(BgView.mas_right).offset(-(kWidth/3-80)/2);
    }];
    [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(BgView.mas_bottom).offset(4);
        make.left.right.bottom.equalTo(weakSelf.view);
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.billData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SettlementCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SettlementCell" forIndexPath:indexPath];
    cell.item = self.billData[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if([cell respondsToSelector:@selector(setLayoutMargins:)]){
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


- (UITableView *)tableV{
    if (_tableV == nil) {
        UITableView *tableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        [tableV setSeparatorColor:DarkColor];
        tableV.rowHeight = 90;
        tableV.delegate = self;
        tableV.dataSource = self;
        [tableV registerNib:[UINib nibWithNibName:@"SettlementCell" bundle:nil] forCellReuseIdentifier:@"SettlementCell"];
        tableV.tableFooterView = [[UIView alloc] init];
        [self.view addSubview:tableV];
        _tableV = tableV;
    }
    return _tableV;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
